<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('原用户')" prop="sourceUser">
            <UserSelect v-model="form.sourceUser.id" :placeholder="$t('原用户')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('交接用户')" prop="handoverUser">
            <UserSelect v-model="form.handoverUser.id" :placeholder="$t('交接用户')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('交接内容')" prop="transferContent">
            <el-checkbox-group v-model="form.transferContent">
              <el-checkbox
                v-for="(dict, index) in transferContentOptions"
                :key="index"
                :label="dict.dictValue"
              >
                {{ $t(dict.dictLabel) }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:handover" />
    </div>
  </div>
</template>

<script>
    import { saveSysUserHandover, getSysUserHandover } from '@/api/system/handover/sysUserHandover'
    export default {
        data() {
            return {
                // 交接内容字典
                transferContentOptions: [],
                // 表单参数
                form: {
                  transferContent: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  transferContent: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
                // 是否隐藏保存等按钮
                btnHide: false
            }
        },
        created() {
          this.getDicts('transfer_content').then(response => {
            this.transferContentOptions = response.data
            console.log(this.transferContentOptions)
          })
          this.init(this.queryParams)
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getSysUserHandover(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    sourceUser: {},
                    handoverUser: {},
                    transferContent: [],
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveSysUserHandover(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                })
           }

        }

    }
</script>
